package com.enjoyiot.module.system.controller.admin.shipin;

import com.enjoyiot.framework.common.pojo.PageResult;
import com.enjoyiot.module.system.controller.admin.shipin.vo.SHPPageReqVO;
import com.enjoyiot.module.system.controller.admin.shipin.vo.SHPRespVO;
import com.enjoyiot.module.system.controller.admin.shipin.vo.SHPSaveReqVO;
import com.enjoyiot.module.system.dal.dataobject.shipin.SHPDO;
import com.enjoyiot.module.system.service.shipin.SHPService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;


import java.io.IOException;
import java.util.List;

import com.enjoyiot.framework.common.pojo.PageParam;
import com.enjoyiot.framework.common.pojo.CommonResult;
import com.enjoyiot.framework.common.util.object.BeanUtils;
import static com.enjoyiot.framework.common.pojo.CommonResult.success;

import com.enjoyiot.framework.excel.core.util.ExcelUtils;

import com.enjoyiot.framework.apilog.core.annotation.ApiAccessLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static com.enjoyiot.framework.apilog.core.enums.OperateTypeEnum.*;



@Tag(name = "管理后台 - 视频监控")
@RestController
@RequestMapping("/sp/SHP")
@Validated
public class SHPController {

    @Resource
    private SHPService sHPService;

    @PostMapping("/create")
    @Operation(summary = "创建视频监控")
    @PreAuthorize("@ss.hasPermission('sp:SHP:create')")
    public CommonResult<Integer> createSHP(@Valid @RequestBody SHPSaveReqVO createReqVO) {
        return success(sHPService.createSHP(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新视频监控")
    @PreAuthorize("@ss.hasPermission('sp:SHP:update')")
    public CommonResult<Boolean> updateSHP(@Valid @RequestBody SHPSaveReqVO updateReqVO) {
        sHPService.updateSHP(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除视频监控")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sp:SHP:delete')")
    public CommonResult<Boolean> deleteSHP(@RequestParam("id") Integer id) {
        sHPService.deleteSHP(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得视频监控")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sp:SHP:query')")
    public CommonResult<SHPRespVO> getSHP(@RequestParam("id") Integer id) {
        SHPDO sHP = sHPService.getSHP(id);
        return success(BeanUtils.toBean(sHP, SHPRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得视频监控分页")
    @PreAuthorize("@ss.hasPermission('sp:SHP:query')")
    public CommonResult<PageResult<SHPRespVO>> getSHPPage(@Valid SHPPageReqVO pageReqVO) {
        PageResult<SHPDO> pageResult = sHPService.getSHPPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SHPRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出视频监控 Excel")
    @PreAuthorize("@ss.hasPermission('sp:SHP:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSHPExcel(@Valid SHPPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SHPDO> list = sHPService.getSHPPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "视频监控.xls", "数据", SHPRespVO.class,
                        BeanUtils.toBean(list, SHPRespVO.class));
    }

}